﻿using HslCommunication;
using HslCommunication.Profinet.Siemens;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Effects;
using TransControlSystem.ViewModel.Common;

namespace TransControlSystem.View
{
    /// <summary>
    /// TagInfoConfigWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TagInfoConfigWindow : Window
    {
        public TagInfoConfigWindow()
        {
            InitializeComponent();
        }
        #region PLC地址
        private readonly string PlcAddress = PlcHelper.GetPlcAddress();
        #endregion PLC地址
        /// <summary>
        /// 主窗体生成列事件
        /// </summary>
        /// <param name="sender">主窗体</param>
        /// <param name="e">事件</param>
        private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Column.Header.ToString() == "Id" ||
               e.Column.Header.ToString() == "EquipmentName" ||
                  e.Column.Header.ToString() == "TagDesc" ||
                  e.Column.Header.ToString() == "Address" ||
                  e.Column.Header.ToString() == "Value" ||
                  e.Column.Header.ToString() == "ValueType" ||
                  e.Column.Header.ToString() == "IsEnable" ||
                  e.Column.Header.ToString() == "OrderNo" ||
                  e.Column.Header.ToString() == "Remark" ||
                  e.Column.Header.ToString() == "TagDesc2" ||
                  e.Column.Header.ToString() == "TagColor" ||
                  e.Column.Header.ToString() == "Type" ||
                  e.Column.Header.ToString() == "Point" ||
                  e.Column.Header.ToString() == "ComTestWriteVisibility" ||
                  e.Column.Header.ToString() == "ReadOrWrite" ||
                  e.Column.Header.ToString() == "IsEnableText" ||
                  e.Column.Header.ToString() == "SomeConditionalProperty" ||
                  e.Column.Header.ToString() == "Background" ||
                  e.Column.Header.ToString() == "ReadOrWriteText"
               )
            {
                e.Cancel = true;
            }
        }
        /// <summary>
        /// 鼠标左键点下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        /// <summary>
        /// 关闭当前窗体
        /// </summary>
        /// <param name="sender">触发事件的对象</param>
        /// <param name="e">事件</param>
        private void Close_Click(object sender, RoutedEventArgs e)
        {
            Environment.Exit(0);
        }

        /// <summary>
        /// 最大化窗体
        /// </summary>
        /// <param name="sender">触发事件的对象</param>
        /// <param name="e">事件</param>
        private void Max_Click(object sender, RoutedEventArgs e)
        {
            WindowStateChange();
        }
        /// <summary>
        /// 窗体状态改变事件处理器 
        /// </summary>
        private void WindowStateChange()
        {
            if (WindowState == WindowState.Maximized)
            {
                WindowState = WindowState.Normal;
            }
            else
            {
                WindowState = WindowState.Maximized;
            }
        }
        /// <summary>
        /// 窗体状态改变事件
        /// </summary>
        /// <param name="sender">触发事件的对象</param>
        /// <param name="e">事件/param>
        private void Window_StateChanged(object sender, EventArgs e)
        {
            DropShadowEffect de = null;
            if (WindowState == WindowState.Normal)
            {
                de = new DropShadowEffect();
                BorderThickness = new Thickness(20);
                de.BlurRadius = 20;
                de.Opacity = .15;
                de.ShadowDepth = 0;
                Effect = de;
            }
            else
            {
                BorderThickness = new Thickness(5);
                Effect = null;
            }
        }
        /// <summary>
        /// 最小化
        /// </summary>
        /// <param name="sender">触发事件单位对象</param>
        /// <param name="e">事件</param>
        private void Min_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }
        /// <summary>
        /// 关闭时事件
        /// </summary>
        /// <param name="sender">触发事件的对象</param>
        /// <param name="e">事件</param>
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            Visibility = Visibility.Hidden;
        }
        /// <summary>
        /// 点击按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Visibility = Visibility.Hidden;
        }
        ////编辑前的值
        //private string BeforeEditValue = string.Empty;
        //private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
        //{
        //    BeforeEditValue = (dg_EditData.SelectedItem as TagInfo).Value;

        //}
        ////编辑后的值
        //private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        //{
        //    string NewValue = (e.EditingElement as TextBox).Text;
        //    //判断新值和旧值是否一样
        //    if (BeforeEditValue != NewValue)
        //    {
        //        if ((dg_EditData.SelectedItem as TagInfo).ValueType == "Bool" && (NewValue == "True" || NewValue == "False" )){
        //            WriteSiemensValByAddressAndType((dg_EditData.SelectedItem as TagInfo).Address,
        //           (dg_EditData.SelectedItem as TagInfo).ValueType,
        //           NewValue);
        //        }else
        //        {
        //            WindowManager.ShowWarning("提示", "尊敬的用户，您修改的值不正确，正确的应该是True或者False。");
        //            (e.EditingElement as TextBox).Text = BeforeEditValue;
        //        }
        //    }
        //}
        /// <summary>
        /// 根据类型使用不同方式写入值 西门子专用
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        private bool WriteSiemensValByAddressAndType(string Address, string ValueType, string Value)
        {
            SiemensS7Net siemens = null;
            OperateResult connect = null;
            // 西门子PLC通讯，指定PLC的ip地址和端口号
            siemens = new SiemensS7Net(SiemensPLCS.S1500, PlcAddress);
            // 连接对象
            connect = siemens.ConnectServer();
            if (!connect.IsSuccess)
            {
                WindowManager.ShowWarning("警告", "与PLC连接中断，请检查网络通讯是否正常！");
            }
            {
                OperateResult operateResult = null;
                if (ValueType == "Bool")
                {
                    operateResult = siemens.Write(Address, bool.Parse(Value.ToLower()));
                }
                else if (ValueType == "DInt")
                {
                    operateResult = siemens.Write(Address, Int32.Parse(Value));
                }
                return operateResult.IsSuccess;
            }
        }
    }
}
